package com.wwt.server.controller;


import com.wwt.server.pojo.Admin;
import com.wwt.server.pojo.Role;
import com.wwt.server.service.IAdminRoleService;
import com.wwt.server.service.IAdminService;
import com.wwt.server.service.IRoleService;
import com.wwt.server.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tao
 * @since 2021-12-05
 */
@RestController
@Api(tags = "操作员管理")
@RequestMapping("system/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IAdminRoleService adminRoleService;

    @ApiOperation("获取操作员名单")
    @GetMapping("/")
    public List<Admin> getAdmin(@RequestParam(value = "keywords", required = false) String keywords) {
        return adminService.getAdmin(keywords);
    }

    @ApiOperation("更新操作员信息")
    @PutMapping("/")
    //@Transactional
    public Result UpdateAdminInfo(@RequestBody Admin admin) {
        if (adminService.updateById1(admin)) {
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作员")
    public Result deleteAdminInfo(@PathVariable Integer id) {
        boolean result = adminService.removeById(id);
        if (result) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @GetMapping("/roles")
    @ApiOperation(value = "获取所有角色")
    public List<Role> getAllRoles() {
        return roleService.list();
    }

    @PutMapping("/role")
    @ApiOperation(value = "更新操作员角色")
    public Result updateRoles(Integer adminId, Integer[] rids) {
        return adminRoleService.updateByAdminId(adminId, rids);
    }

}
